// pages/musicSearch/musicSearch.js
Page({
  data: {
    songs: []
  },
  onLoad: function (options) {

  },
  async onSubmit(event) {
    var keyword = event.detail.value.name;
    var module = await import("../../utils/http");
    var searchRes = await module.MusicHttp.searchSong(keyword);
    this.handleHttpData(searchRes);
  },
  handleHttpData(res) {
    var arr = [];
    res.data.result.songs.forEach(async item => {
      var { id, name } = item;
      var artistsName;
      item.artists.forEach(item => {
        artistsName = item.name;
      })
      var module = await import("../../utils/http");
      var detailRes = await module.MusicHttp.searchDetail(id);
      detailRes.data.songs.forEach(item => {
        arr.push({ id, name, artistsName, picUrl: item.al.picUrl });
      })
      this.setData({ songs: arr })
    })
  },
  toSong(event) {
    var id = event.currentTarget.dataset.id;
    var url = event.currentTarget.dataset.url;
    wx.setStorageSync('url', url)
    wx.navigateTo({
      url: `/pages/song/song?id=${id}`,
    })
  }
})